function pval =binotest2(numSuccess, numTrials)
%Written By: Jeff Turner
%this function computes p value using the binomial test for a given
%number of successes in a given number of trials
pval = 0;
interval=20;
if numSuccess < numTrials/2
    numSuccess = numTrials - numSuccess;
end
for n = numSuccess:numTrials,
    perm = 1;
    numIntervals=floor(min((numTrials-n)/interval,n/interval));
    for i = 1:numIntervals,
        perm = perm*(prod(numTrials-i*interval+1:numTrials-(i-1)*interval)/prod(numTrials - n - i*interval+1:numTrials - n -(i-1)*interval));
    end
    perm = perm*(prod(n+1:numTrials-numIntervals*interval)/factorial(numTrials - n - numIntervals*interval));
    pval = pval + perm*0.5^numTrials;
end
return
%1-binocdf(max(numTrials-numSuccess-1,numSuccess-1),numTrials,.5);
